/*
 * The MIT License
 *
 * Copyright 2015 Gavin Crayk.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package com.googlecode.census.client.message;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.apache.commons.lang3.builder.StandardToStringStyle;
import org.apache.commons.lang3.builder.ToStringBuilder;

/**
 *
 * <pre>
 * {
 *  "action":"subscribe",
 *  "characters":[
 *      "1",
 *      "2"
 *  ],
 *  "eventNames":[
 *      "BattleRankUp",
 *      "Death",
 *      "FacilityControl",
 *      "GainExperience",
 *      "ItemAdded",
 *      "MetagameEvent",
 *      "PlayerLogin",
 *      "PlayerLogout",
 *      "SkillAdded",
 *      "VehicleDestroy"
 *  ],
 *  "service":"event",
 *  "worlds":[
 *      "1",
 *      "2"
 *  ]
 * }
 * </pre>
 *
 * @author Gavin Crayk
 */
public final class SubscriptionMessage {

    private final String action;
    private final String[] characters;
    private final String[] eventNames;
    private final String service;
    private final String[] worlds;

    public static class Builder {

        private final String action = "subscribe";
        private final String service = "event";
        private final List<String> characters = new ArrayList<>();
        private final List<String> eventNames = new ArrayList<>();
        private final List<String> worlds = new ArrayList<>();

        public Builder character(final String... characters) {
            this.characters.addAll(Arrays.asList(characters));
            return this;
        }

        public Builder eventName(final String... eventNames) {
            this.eventNames.addAll(Arrays.asList(eventNames));
            return this;
        }

        public Builder world(final String... worlds) {
            this.worlds.addAll(Arrays.asList(worlds));
            return this;
        }

        public SubscriptionMessage toMessage() {
            return new SubscriptionMessage(
                    action, 
                    characters.toArray(new String[characters.size()]), 
                    eventNames.toArray(new String[eventNames.size()]), 
                    service, 
                    worlds.toArray(new String[worlds.size()]));
        }
    }

    private SubscriptionMessage(
            final String action,
            final String[] characters,
            final String[] eventNames,
            final String service,
            final String[] worlds) {
        this.action = action;
        this.characters = characters;
        this.eventNames = eventNames;
        this.service = service;
        this.worlds = worlds;
    }

    @Override
    public String toString() {
        return new ToStringBuilder(this, StandardToStringStyle.SHORT_PREFIX_STYLE)
                .append("action", action)
                .append("characters", characters)
                .append("eventNames", eventNames)
                .append("service", service)
                .append("worlds", worlds)
                .toString();
    }
}
